define(['moment'], function () {
    return ['$scope', '$rootScope', '$filter', '$stateParams', '$mdDialog', 'config', 'MMWService', '$calculater',
        function ($scope, $rootScope, $filter, $stateParams, $mdDialog, config, $MMWService, $calculater) {

            $scope.init = function () {
                $scope.material_no = '';
                $scope.isSelectAll = false;
                $scope.new_reel_list=[];
                $scope.origin_reel_list=[];
            };

            $scope.loadMaterialNo = function(){
                $rootScope.showQueryList({
                    pageRecord: 50,
                    header: [
                        { label: $filter('translate')('SMT2503.list1.material_no'), code: 'material_no' },
                        { label: $filter('translate')('SMT2503.list1.material_name'), code: 'material_name' }
                    ],
                    query: function (conditions, feedback) {
                        $MMWService.sendToServer({
                            uri: 'ServicesSMT.Module_SMT.reel_combine_material_info_get',
                            content: {},
                            success: function (data) {
                                feedback(data.material_list);
                            }
                        });
                    },
                    selectCode: $scope.material_no,
                    confirm: function (selectItem, dialog) {
                        $scope.material_no = selectItem.material_no;
                        $scope.material_name = selectItem.material_name;
                        dialog.hide();
                    }
                });
            }

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

             // QRCode輸入
             $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        $scope.new_reel_no = result.text;
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            $scope.search = function () {
                $MMWService.sendToServer({
                    uri: 'ServicesSMT.Module_SMT.reel_combine_info_get',
                    content: {
                        material_no:$scope.material_no,
                    },
                    success: function (data) {
                        $scope.origin_reel_list = data.reel_list;
                        $scope.origin_reel_list.forEach(function(item){
                            item.material_no = $scope.material_no;
                            item.btn_mode = false;
                        })
                        $scope.material_no = '';
                    }
                });
            };

            $scope.selectAll = function(){
                $scope.isSelectAll = !$scope.isSelectAll;
                if($scope.isSelectAll){
                    $scope.origin_reel_list.forEach(function(item){
                        item.btn_mode = true;
                    })
                }else{
                    $scope.origin_reel_list.forEach(function(item){
                        item.btn_mode = false;
                    })
                }
            }

            $scope.combine = function(){
                let combine_arr = $scope.origin_reel_list.filter(function(item){
                    return item.btn_mode;
                })
                let content = {
                    new_reel_no:$scope.new_reel_no,
                    combine_reason:$scope.combine_reason,
                    reel_list:combine_arr
                }
                $MMWService.sendToServer({
                    uri: 'ServicesSMT.Module_SMT.reel_combine_process',
                    content,
                    success: function (data) {
                        $scope.new_reel_list = data.new_reel_list;
                        $scope.origin_reel_list=[];
                    }
                });
            }

            $scope.deleteItem = function(item,key){
                let content={
                    new_reel_list:[item]
                }
                $MMWService.sendToServer({
                    uri: 'ServicesSMT.Module_SMT.reel_combine_cancel_process',
                    content,
                    success: function (data) {
                        $scope.new_reel_list.splice(key,1);
                        $scope.origin_reel_list=[];
                    }
                });
            }
        }
    ];
});
